﻿using System;
using System.Linq;
using System.Collections.Generic;
using CoachingOrganizer.Database.DataAccess;
using CoachingOrganizer.Toolkit.Events;

namespace CoachingOrganizer.Database.DataAccessLayer
{
    public class MainDAL
    {
        private readonly DbManagerBase _dbManager;
        private readonly List<PeriodDAL> _periods = new List<PeriodDAL>();

        public DbManagerBase DbManager
        {
            get { return _dbManager; }
        }

        public List<PeriodDAL> Periods
        {
            get { return _periods; }
        }

        public MainDAL(DbManagerBase dbManager)
        {
            _dbManager = dbManager;
        }

        public PeriodDAL CreateNewPeriod()
        {
            return new PeriodDAL(_dbManager);
        }

        public void AddPeriod(PeriodDAL period)
        {
            _periods.Add(period);
            _dbManager.Context.Insert(period.CreateNewDA());
        }

        public void LoadPeriods()
        {
            foreach (PeriodDA periodDA in _dbManager.Context.Table<PeriodDA>().ToArray())
            {
                PeriodDAL period = new PeriodDAL(_dbManager);
                period.Load(periodDA);
                _periods.Add(period);
            }
        }
    }
}

